﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class orgman_users_new : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        MasApp.BLL.Check.CheckLogin();
        tb_username.Focus();
    }
    protected void tb_username_TextChanged(object sender, EventArgs e)
    {
        if (MasApp.BLL.Search.HaveUsername(tb_username.Text))
        {
            Label1.Visible = true;
        }
        else
        {
            Label1.Visible = false;
        }
    }
    protected void ImageButton1_Click(object sender, EventArgs e)
    {
        if (cb_level.Checked || cb_role4.Checked || cb_role6.Checked || cb_weight.Checked)
        {
            #region Model us
            MasApp.Model.users us = new MasApp.Model.users();
            us.Address = tb_address.Text;
            us.Description = tb_description.Text;
            us.Email = tb_email.Text;
            us.Mphone = tb_mphone.Text;
            us.Organizationid = MasApp.BLL.Search.SearchOrgID(Session["username"].ToString());
            us.Phone = tb_phone.Text;
            us.Postcode = tb_postcode.Text;
            us.Realname = tb_realname.Text;
            us.Sex = DropDownList1.SelectedValue;
            us.Username = tb_username.Text;
            #endregion
            #region Model log
            MasApp.Model.login log = new MasApp.Model.login();
            log.Username = tb_username.Text;
            log.Password = tb_password.Text;
            #endregion
            //插入用户和登录信息
            bool regUsLog = MasApp.BLL.reg.InsertUsLog(log, us);
            //插入用户角色
            #region Model ur
            MasApp.Model.userrole ur = new MasApp.Model.userrole();
            ur.Userid = MasApp.BLL.Search.SearchUserID(tb_username.Text);

            bool regrolelevel , regroleweight , regrole4 , regrole6 ;
            regrolelevel = regroleweight = regrole4 = regrole6 = true; 

            if (cb_level.Checked)
            {
                ur.Roleid = 8;
                regrolelevel = (MasApp.BLL.reg.RegUserRolebool(ur) == true) ? true : false;
            }
            if (cb_role6.Checked)
            {
                ur.Roleid = 6;
                regrole6 = (MasApp.BLL.reg.RegUserRolebool(ur) == true) ? true : false;
                cb_role4.Checked = true;
            }
            if (cb_role4.Checked)
            {
                ur.Roleid = 4;
                regrole4 = (MasApp.BLL.reg.RegUserRolebool(ur) == true) ? true : false;
            }
            if (cb_weight.Checked)
            {
                ur.Roleid = 7;
                regroleweight = (MasApp.BLL.reg.RegUserRolebool(ur) == true) ? true : false;
            }
            #endregion

            if (regUsLog && regroleweight && regrolelevel && regrole6 && regrole4)
            {
                MasApp.BLL.ResponseHelper.alert("用户【 " + tb_username.Text + " 】添加成功！", "new.aspx");
            }
            else
            {

                MasApp.BLL.ResponseHelper.alert("操作失败！", "new.aspx");
            }
        }
        else
        {
            MasApp.BLL.ResponseHelper.alert("为选择任何用户角色，请选择至少一种角色！");
        }
    }
}
